/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.server.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class HttppostUtil {

    static Logger logger = Logger.getLogger(HttppostUtil.class.getName());

//    public static String sendJson(String url, String json, String post) {
//        String result = "";
//        HttpPost httppost = null;
//        try {
//            //默认字符编码是ISO-8859-1
//            HttpClient httpClient = new DefaultHttpClient();
//
//            httppost = new HttpPost(post);
//
//            InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1);
//            reqEntity.setContentType("application/octet-stream");// 区分不同种类的数据,xml数据
//            reqEntity.setContentEncoding("UTF-8");// charset=UTF-8
//            httppost.setEntity(reqEntity);
//
//            HttpResponse response = httpClient.execute(httppost);
//            StatusLine sl = response.getStatusLine();
//
//            if (sl.getStatusCode() == 200) {
////                if (true) {
////
////                }
////                String rs = HttpResponseParser.getResponseContent(response);
////                logger.info(rs);
////                result = rs;
//                File file = new File("/home/ycl/project/TestClient/audio/client/" + string2MD5(url) + suffix(url));
//                if (!file.exists()) {
//                    file.createNewFile();
//                    logger.info(file.getAbsolutePath());
//                }
//                FileOutputStream fileOutputStream = new FileOutputStream(file);
//                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//                int bytesReadIn = 0;
//                byte[] bytesContent = new byte[1024 * 2];
//                int fileSize = 0;
//                while ((bytesReadIn = response.getEntity().getContent().read(bytesContent, 0, 1024 * 2)) > 0) {
//                    bos.write(bytesContent, 0, bytesReadIn);
//                    fileSize += bytesReadIn;
//                }
//                if (fileSize < 1000) {
//                    logger.info("size < 1000");
//                }
//                bos.flush();
//                fileOutputStream.close();
//                bos.close();
//            } else {
//                result = HttpResponseParser.getResponseContent(response);
//                logger.info("client fail " + result);
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            logger.error(ex.getMessage(), ex);
//        } finally {
//            httppost.releaseConnection();
//        }
//        return result;
//
//    }

    public static String suffix(String url) {
        String suffixestr = ".m4a|.avi|.mpeg|.3gp|.mp3|.mp4|.wav|.jpeg|.gif|.jpg|.png|.apk|.exe|.pdf|.rar|.zip|.docx|.doc";
        String suffix = null;
        try {

//            String suffixestr = RuntimeEnv.getParam(RuntimeEnv.suffixestr) + "";
            Pattern pat = Pattern.compile(suffixestr);
            Matcher d = pat.matcher(url);
            //注意d.find()只能用一次，否则将查找不到
//            System.out.println("find "+d.find()+","+d.group());
            if (d.find()) {
                suffix = d.group();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return suffix;
    }

    public static String string2MD5(String plaintext) {
        MessageDigest m = null;
        try {
            m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    }
}
